package cn.iocoder.yudao.module.ems.service.supplier;

import cn.iocoder.yudao.module.ems.controller.admin.supplier.vo.ContactCreateReqVO;
import cn.iocoder.yudao.module.ems.controller.admin.supplier.vo.ContactRespVO;
import cn.iocoder.yudao.module.ems.convert.supplier.ContactConvert;
import cn.iocoder.yudao.module.ems.dal.dataobject.spares.ContactDO;
import cn.iocoder.yudao.module.ems.dal.dataobject.spares.SparesDO;
import cn.iocoder.yudao.module.ems.dal.mysql.spares.SparesMapper;
import cn.iocoder.yudao.module.ems.dal.mysql.supplier.ContactMapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.List;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.ems.enums.ErrorCodeConstants.SPARES_ITEMNO_NOTEXISTS;
import static cn.iocoder.yudao.module.ems.enums.ErrorCodeConstants.WAREHOUSING_NOT_EXISTS;

/**
 * 入库信息 Service 实现类
 *
 * @author dason
 */
@Service
@Validated
public class ContactServiceImpl implements ContactService {

    @Resource
    private ContactMapper contactMapper;

    private void validateContactExists(Long id) {
        if (contactMapper.selectById(id) == null) {
            throw exception(WAREHOUSING_NOT_EXISTS);
        }
    }

    @Override
    public List<ContactDO> getContactList(Long warehousingId) {
        return contactMapper.selectList(warehousingId);
    }

    @Override
    public void deleteContact(Long id) {
        // 校验存在
        this.validateContactExists(id);
        // 删除
        contactMapper.deleteById(id);
    }


    @Override
    public void updateContact(ContactRespVO updateReqVO) {
        // 校验存在
        this.validateContactExists(updateReqVO.getId());
        // 更新
        ContactDO updateObj = ContactConvert.INSTANCE.convert3(updateReqVO);
        contactMapper.updateById(updateObj);
    }

}
